package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.entity.order.flight.FlightCatchOrder;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.entity.misc.Product;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.qms.service.EnterpriseService;
import cn.buk.qms.service.FlightCatchOrderService;
import cn.buk.tms.flight.request.FlightOrderRequestDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

import static cn.buk.qms.HttpServletUtil.getIpAddr;

/**
 * 机票追位订单 控制器
 * @author yfdai
 */
@RestController
@RequestMapping("/catch")
public class CatchOrderController extends BaseController {

    @Autowired
    public CatchOrderController(FlightCatchOrderService service,
                                EnterpriseService enterpriseService) {
        this.service = service;
        this.enterpriseService = enterpriseService;
    }

    private final FlightCatchOrderService service;

    private final EnterpriseService enterpriseService;

    /**
     * 创建追位订单
     */
    @PostMapping(value = "/flight/new", consumes = "application/json")
    public JsonResult createFlightTicketBill(HttpServletRequest request,
                                             @RequestBody FlightOrderRequestDto dto) {
        final int enterpriseId = getEnterpriseId(request);
        final String operator = getUsername(request);
        final String remoteAddress = getIpAddr(request);

        return service.createOrder(enterpriseId, operator, remoteAddress, dto);
    }

    /**
     * 查找机票追位订单
     */
    @RequestMapping("/flights")
    public CommonDto<FlightCatchOrder> searchFlightOrders(HttpServletRequest request,
                                                                @ModelAttribute("sc") SearchFlightOrderCriteria sc
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);
        final String username = getUsername(request);

        List<FlightCatchOrder> datas = service.searchFlightOrders(enterpriseId, userId, username, sc);

        CommonDto<FlightCatchOrder> commonDto = new CommonDto<>();
        commonDto.setDataList(datas);
        commonDto.setPage(sc.getPage());

        return commonDto;
    }


    /**
     * 获取机票订单详情
     */
    @RequestMapping("/flight/{id}")
    public FlightCatchOrder getFlightOrderDetail(HttpServletRequest request,
                                                     @PathVariable("id") int id
    ) {
        final int enterpriseId = getEnterpriseId(request);

        return service.getFlightOrderDetail(enterpriseId, id);
    }

    /**
     * 删除追位订单
     */
    @RequestMapping("/flight/{orderId}/removed")
    public JsonResult deleteCatchOrder(HttpServletRequest request,
                                                       @PathVariable("orderId") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);

        return service.deleteCatchOrder(enterpriseId, orderId);
    }

    /**
     * 获取机票订单操作记录
     */
    @RequestMapping("/flight/{orderId}/history")
    public List<OperationLog> searchFlightOrderHistory(HttpServletRequest request,
                                                       @PathVariable("orderId") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);

        return service.searchFlightOrderHistory(enterpriseId, orderId);
    }


    /**
     * 修改机票订单的乘机人信息
     */
    @PostMapping(value = "/flight/{orderId}/passenger")
    public JsonResult updateFlightOrderPsg(HttpServletRequest request,
                                             @PathVariable("orderId") int orderId,
                                             @RequestParam("id") int psgId,
                                           @RequestParam("name") String psgName,
                                           @RequestParam("idNo") String psgIdNo,
                                           @RequestParam("ffpNo") String psgFfpNo,
                                           @RequestParam(value = "mobile", required = false, defaultValue = "") String psgMobile) {
        final int enterpriseId = getEnterpriseId(request);
        return service.updateFlightOrderPassenger(enterpriseId, orderId, psgId, psgName, psgIdNo, psgFfpNo, psgMobile);
    }

    /**
     * 修改机票订单的备注
     */
    @PostMapping(value = "/flight/{orderId}/remark")
    public JsonResult updateFlightOrderRemark(HttpServletRequest request,
                                           @PathVariable("orderId") int orderId,
                                           @RequestParam("remark") String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateFlightOrderRemark(enterpriseId, username, orderId, remark);
    }

    /**
     * 修改机票订单的编码
     */
    @PostMapping(value = "/flight/{orderId}/pnr")
    public JsonResult updateFlightOrderPnr(HttpServletRequest request,
                                              @PathVariable("orderId") int orderId,
                                              @RequestParam("pnrNo") String pnrNo) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateFlightOrderPnr(enterpriseId, username, orderId, pnrNo);
    }

    /**
     * 申请取消/取消订单
     * @param orderId 订单id
     * @param reasonCode 原因代码
     * @param reasonDesc 原因描述
     */
    @PostMapping(value = "/flight/{orderId}/status/canceled")
    public JsonResult cancelFlightOrder(HttpServletRequest request,
                                              @PathVariable("orderId") int orderId,
                                        @RequestParam(name = "reasonCode", defaultValue = "0") int reasonCode,
                                        @RequestParam(name = "reasonDesc") String reasonDesc
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.cancelFlightOrder(enterpriseId, username, orderId, reasonCode, reasonDesc);
    }

    /**
     * 重新启用监控
     * @param orderId 订单id
     */
    @PostMapping(value = "/flight/{orderId}/status/monitoring")
    public JsonResult cancelFlightOrder(HttpServletRequest request,
                                        @PathVariable("orderId") int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.monitorFlightCatchOrder(enterpriseId, username, orderId);
    }

    /**
     * 追位功能的使用期限
     */
    @PostMapping(value = "/flight/expireDate")
    public JsonResult checkProductExpireDate(HttpServletRequest request) {
        final int enterpriseId = getEnterpriseId(request);

        return enterpriseService.checkProductExpireDate(enterpriseId, Product.ETERM_AUTO_SS);
    }

}
